﻿using Simple.ISysRepository;
using Simple.ISysService;
using Simple.Tools;
using Simple.Tools.Extensions;
using Simple.Tools.Verify;
using SimpleSaas.Common;
using SimpleSaas.Common.Execption;
using SimpleSaas.Entity;
using SimpleSaas.Entity.Dto;
using SimpleSaas.Entity.View;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Simple.SysService
/// <summary>
/// 版 本 钢易智企2.0 V 2.0.0 钢易智企2.0
/// Copyright (c) 2023 杭州钢易科技有限公司
/// 创建人：曾东明
/// 日 期：2024
/// 描 述：
/// </summary>
{
	/// <summary>
	/// 
	/// </summary>
	public class SysMenuService : ISysMenuService
	{

		[Autowired]
		protected ISysMenuRepository sysMenuRepository { get; set; }

		[Autowired]
		protected ISysAppRepository sysAppRepository { get; set; }
		[Autowired]
		protected ISysTenantMenuRepository sysTenantMenuRepository { get; set; }

		[Autowired]
		protected ISimpleMapper simpleMapper { get; set; }
		


		public SysMenuService()
		{
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="sysMenuDto"></param>
		/// <returns></returns>
		/// <exception cref="BusinessException"></exception>
		public async Task AddAsync(SysMenuDto sysMenuDto)
		{
			var verify = sysMenuDto.Verify();

			if (!verify.Item2)
			{
				throw new BusinessException(verify.Item1);
			}

			if (await sysMenuRepository.AnyAsync(menu => menu.MenuName.Equals(sysMenuDto.MenuName)))
			{
				throw new BusinessException("菜单名称重复!");
			}

			if (await sysAppRepository.AnyAsync(app => app.Id.Equals(sysMenuDto.AppId)))
			{
				throw new BusinessException("请选择app!");
			}
			SysMenuEntity sysMenu = new SysMenuEntity();

			simpleMapper.Map(sysMenuDto, sysMenu);

			await sysMenuRepository.CreateAsync(sysMenu);
		}

		/// <summary>
		///  更新菜单
		/// </summary>
		/// <param name="sysMenuDto"></param>
		/// <returns></returns>
		/// <exception cref="BusinessException"></exception>
		public async Task UpdateAsync(SysMenuDto sysMenuDto)
		{
			var verify = sysMenuDto.Verify();

			if (!verify.Item2)
			{
				throw new BusinessException(verify.Item1);
			}

			if (await sysMenuRepository.AnyAsync(menu => menu.MenuName.Equals(sysMenuDto.MenuName)))
			{
				throw new BusinessException("菜单名称重复!");
			}

			if (await sysAppRepository.AnyAsync(app => app.Id.Equals(sysMenuDto.AppId)))
			{
				throw new BusinessException("请选择app!");
			}
			SysMenuEntity sysMenu = await sysMenuRepository.FirstOrDefaultAsync(menu => menu.Id == sysMenuDto.Id.ToInt64());

			simpleMapper.Map(sysMenuDto, sysMenu);

			await sysMenuRepository.UpdateAsync(sysMenu);
		}

		/// <summary>
		///  软删除
		/// </summary>
		/// <param name="sysMenuDto"></param>
		/// <returns></returns>
		/// <exception cref="BusinessException"></exception>
		public async Task SoftDeleteAsync(SysMenuDto sysMenuDto)
		{

			if (await sysTenantMenuRepository.AnyAsync(r => r.MenuId == sysMenuDto.Id.ToInt64()))
			{
				throw new BusinessException("删除失败已经绑定企业!");
			}

			await sysMenuRepository.SoftDeleteAsync(menu => menu.Id == sysMenuDto.Id.ToInt64());
		}

		/// <summary>
		///  分页查询
		/// </summary>
		/// <param name="sysMenuDto"></param>
		/// <returns></returns>
		/// <exception cref="BusinessException"></exception>
		public async Task<PageList<SysMenuView>> GetPageAsync(SysMenuParameter Params)
		{

			var query = sysMenuRepository.Query(q =>
			{
				if (Params.AppId.ToInt64() > 0)
				{
					q.Where(menu => menu.AppId.Equals(Params.AppId.ToInt64()));
				}
				q.Where(menu => !menu.IsDeleted);
			});
			return await PageList<SysMenuView>.CreateAsync(query, Params);
		}

		/// <summary>
		///  查询单条
		/// </summary>
		/// <param name="sysMenuDto"></param>
		/// <returns></returns>
		/// <exception cref="BusinessException"></exception>
		public async Task<SysMenuView> GetByIdAsync(SysMenuParameter Params)
		{

			return await sysMenuRepository.Query(q =>
			{
				q.Where(menu => menu.Id.Equals(Params.Id.ToInt64()) && !menu.IsDeleted);

			}).FirstAsync();
		}
	}
}
